﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyCuaHangBanMayTinh.He_Thong_Xu_Ly;
using System.Data;
using System.Security.Cryptography;

namespace QuanLyCuaHangBanMayTinh.He_Thong_Du_Lieu
{
    class NguoiDungDAO
    {
        private static string Hash(string password)
        {
            SHA1Managed sha1 = new SHA1Managed();
            byte[] buffer = sha1.ComputeHash(Encoding.UTF8.GetBytes(password));
            return BitConverter.ToString(buffer);
        } 

        static public DataTable SelectmhAll()
        {
            string sql = "select * from NGUOIDUNG";
            DataTable dt = DataAccess.ExecQuery(sql);
            return dt;
        }
        static public void Insert(NguoiDungDTO nd)
        {
            string sql = "insert into NGUOIDUNG(TenNguoiDung,MatKhauNguoiDung,PhanQuyen) values('" + nd.TenNguoiDung + "','" + nd.MatKhauNguoiDung + "','" + nd.PhanQuyen + "')";
            DataAccess.ExecNonQuery(sql);
        }

        static public void Update(NguoiDungDTO nd)
        {

        }

        static public void Delete(NguoiDungDTO nd)
        {
        }

        static public NguoiDungDTO SelectmhByName(string name)
        {
            string sql = "select * from NGUOIDUNG where TenNguoiDung = '" + name + "'";
            DataTable dt = DataAccess.ExecQuery(sql);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                NguoiDungDTO nd = new NguoiDungDTO();
                nd.TenNguoiDung = dt.Rows[0].ItemArray[1].ToString();
                nd.MatKhauNguoiDung = dt.Rows[0].ItemArray[1].ToString();
                nd.PhanQuyen = dt.Rows[0].ItemArray[1].ToString();
                return nd;
            }
        }
        public static bool KiemTraNguoiDung(NguoiDungDTO user)
        {
            string pass = Hash(user.MatKhauNguoiDung);
            //            string pass = user.MatKhau;
            string sql = "select * from NGUOIDUNG where TenNguoiDung = '" + user.TenNguoiDung + "' and MatKhauNguoiDung = '" + pass + "'";
            DataTable dt = DataAccess.ExecQuery(sql);
            if (dt.Rows.Count != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static NguoiDungDTO LayThongTinNguoiDung(string TenNguoiDung)
        {
            string sql = "SELECT * from NGUOIDUNG where TenNguoiDung = '" + TenNguoiDung + "'";
            DataTable dt = DataAccess.ExecQuery(sql);
            if (dt.Rows.Count != 0)
            {
                NguoiDungDTO user = new NguoiDungDTO();
                user.TenNguoiDung = TenNguoiDung;
                user.MatKhauNguoiDung = dt.Rows[0][1].ToString();
                user.PhanQuyen = dt.Rows[0][2].ToString();
                return user;
            }
            return null;
        }

        public static DataTable LayDanhSachNguoiDung()
        {
            string sql = "Select * from NGUOIDUNG";
            return DataAccess.ExecQuery(sql);
        }
    }
}
